package com.hyts.infodb.web;

import com.hyts.infodb.utils.ServletUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

/**
 * 验证码
 * 
 * @author ligaofu
 *
 */
@RestController
@RequestMapping("/verify")
public class VerifyCodeController {

	private final static String ValidateCode = "VALIDATECODE";

	/**
	 * 获得验证码
	 */
	@RequestMapping("/verifyCode")
	public  void verifyCode() {
		HttpServletResponse response = ServletUtil.getResponse();
		HttpServletRequest request = ServletUtil.getRequest();
		HttpSession session = request.getSession();

		response.setContentType("image/jpeg");// 设置相应类型,告诉浏览器输出的内容为图片
		response.setHeader("Pragma", "No-cache");// 设置响应头信息，告诉浏览器不要缓存此内容
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expire", 0);
		//
		RandomValidateCode validateCode = new RandomValidateCode();
		session.removeAttribute(ValidateCode);
		session.setAttribute(ValidateCode, validateCode.getValidateCode());
		try {
			ImageIO.write(validateCode.getValidateCodeImg(), "JPEG",
					response.getOutputStream());// 将内存中的图片通过流动形式输出到客户端
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 判断输入的验证码是否正确
	 */
	@RequestMapping("/checkVerifyCode")
	public @ResponseBody
    void checkVerifyCode() {
		String pVerifyCode = ServletUtil.getRequest()
				.getParameter("verifyCode");
		String sVerifyCode = (String) ServletUtil.getSession().getAttribute(
				ValidateCode);

		if (sVerifyCode.equalsIgnoreCase(pVerifyCode)) {
			try {
				ServletUtil.getResponse().getWriter()
						.print("success:checkVerifyCode");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 校验验证码的静态方法
	 * 
	 * @param verifyCode
	 *            验证码
	 * @return
	 */
	public static boolean checkVerifyCode(String verifyCode) {
		String sVerifyCode = (String) ServletUtil.getSession().getAttribute(
				ValidateCode);
		if (sVerifyCode.equalsIgnoreCase(verifyCode)) {
			return true;
		} else {
			return false;
		}
	}


	/**
	 * 随机验证码生成类
	 *
	 * @author gaofu
	 *
	 */
	public static class RandomValidateCode {

		private String validateCode; // 生成的验证码字符串
		private BufferedImage validateCodeImg; // 生成的验证码图片
		//
		private int imgWidth = 100;// 图片宽
		private int imgHeight = 30;// 图片高
		private int lineSize = 5;// 干扰线数量
		private int codeNum = 5;// 随机产生字符数量
		//
		private Random random = new Random();
		private String randomScope = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";// 随机范围

		public RandomValidateCode() {
			init();
		}

		// 初始化
		private void init() {
			// BufferedImage类是具有缓冲区的Image类,Image类是用于描述图像信息的类
			validateCodeImg = new BufferedImage(imgWidth, imgHeight,
					BufferedImage.TYPE_INT_BGR);
			Graphics graphics = validateCodeImg.getGraphics();// 产生Image对象的Graphics对象,该对象可以在图像上进行各种绘制操作
			graphics.fillRect(0, 0, imgWidth, imgHeight);
			graphics.setFont(new Font("Times New Roman", Font.ROMAN_BASELINE, 18));
			graphics.setColor(getRandColor(110, 133));
			// 绘制干扰线
			for (int i = 0; i <= lineSize; i++) {
				drowLine(graphics);
			}
			// 绘制随机字符
			validateCode = "";
			int beginX = random.nextInt(imgWidth - 17 * codeNum);
			for (int i = 0; i < codeNum; i++) {
				validateCode += drowChar(graphics, i, beginX);
			}
			graphics.dispose();
		}

		public BufferedImage getValidateCodeImg() {
			return validateCodeImg;
		}

		public String getValidateCode() {
			return validateCode;
		}

		/*
		 * 获得字体
		 */
		private Font getFont() {
			return new Font("Fixedsys", Font.CENTER_BASELINE, 22);
		}

		/*
		 * 获得颜色
		 */
		private Color getRandColor(int fc, int bc) {
			if (fc > 255)
				fc = 255;
			if (bc > 255)
				bc = 255;
			int r = fc + random.nextInt(bc - fc - 16);
			int g = fc + random.nextInt(bc - fc - 14);
			int b = fc + random.nextInt(bc - fc - 18);
			return new Color(r, g, b);
		}

		/**
		 * 绘制字符
		 */
		private char drowChar(Graphics g, int i, int beginX) {
			char rand = getRandomChar();
			//
			g.setFont(getFont());
			g.setColor(new Color(random.nextInt(101), random.nextInt(121), random
					.nextInt(141)));
			g.translate(random.nextInt(3), random.nextInt(3));
			g.drawString(String.valueOf(rand), beginX + 15 * i,
					15 + random.nextInt(10));
			return rand;
		}

		/**
		 * 绘制干扰线
		 */
		private void drowLine(Graphics g) {
			int x = random.nextInt(imgWidth);
			int y = random.nextInt(imgHeight);
			int xl = random.nextInt(13);
			int yl = random.nextInt(15);
			g.drawLine(x, y, x + xl, y + yl);
		}

		/**
		 * 获取随机的字符
		 */
		private char getRandomChar() {
			int num = random.nextInt(randomScope.length());
			return randomScope.charAt(num);
		}

	}
}
